// pages/map/map.js
import Api from '../../utils/api';
import util from '../../utils/util';
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    searchCont:'搜索附近店铺',
    shopList: [], //附近的商铺列表
    latitude: '',
    longitude: '',
    markers: [],
    points: [], //
    isShopList: true, //标记是否有店铺
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let _this = this;
    if (wx.getStorageSync('originalData')) {
      let originalData = wx.getStorageSync('originalData');
      _this.getNearby(originalData.location.lng, originalData.location.lat);
    }
   
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    app.globalData.otherPageHideFlag = true;
  },

  //获取5公里内的商铺
  getNearby(lng, lat){ //接受的是百度经纬度
    let _this = this;
    let txMap = util.bMapTransTXmap(lng, lat);
    console.log(txMap)
    _this.setData({
      longitude: txMap.lng,
      latitude: txMap.lat,
      points: [{
        longitude: txMap.lng,
        latitude: txMap.lat,
      }]
    })
    console.log(_this.data.points)
    let data = {
      longitude: lng,
      latitude: lat,
    }
    Api.getNearby(data).then( res => {
      console.log(res)
      _this.setData({
        shopList: res.data
      })
      let markers = [{id: 1,latitude: lat,longitude: lng,iconPath: '/img/location.png',}] //默认有第一个表示搜索出来的当前位置
      _this.data.shopList.forEach((item,index,arr) => {
        let txMap = util.bMapTransTXmap(item.retailShop.longitude, item.retailShop.latitude);
        let marker = {
          id: item.retailShop.id,
          latitude: txMap.lat,
          longitude: txMap.lng,
          iconPath: '/img/location-shop.png',
          title: item.shopName,
        }
        markers.push(marker)
      })
      _this.setData({
        markers: markers
      })
      if (markers.length <= 1){
        _this.setData({
          isShopList: false
        })
      }else{
        _this.setData({
          isShopList: true
        })
      }
      //console.log(_this.data.markers)
    })
  },

  //点击标记的图标
  markertap(e){
    // console.log(e.markerId)
    if (e.markerId != 1){
      wx.navigateTo({
        url: `/pages/shopClass/shopClass?id=${e.markerId}`,
      })
    }
  },

  //搜索地理位置
  bindRegionChange(e){
    console.log(e)
    let _this = this;
            app.globalData.otherPageHideFlag = true;
    wx.chooseLocation({
      success: function(res) {
        console.log(res.address) 
        _this.setData({
          searchCont: res.address
        })
        let bMap = util.tXmapTransBMap(res.longitude, res.latitude);
        //console.log(bMap)
        _this.getNearby(bMap.lng, bMap.lat);
        wx.request({
          url: `https://api.map.baidu.com/cloudgc/v1?ak=iOYBmDKHicYmzZigGIGnurrPf1YMKlEj&address=${res.address}`,
          method: 'GET',
          success: function (res) {
            console.log("百度api", res)
            if (res.data.result[0]) {
              let result = res.data.result[0];
              let originalData = wx.getStorageSync('originalData');
              originalData.formatted_address = result.formatted_address;
              originalData.location = result.location;
              originalData.addressComponent = result.address_components;
              wx.setStorageSync('originalData', originalData);
            }

          },
          fail: function (res) {
            console.log(res)
          },
          complete: function (res) {
            console.log(res)
          },
        });
      },
      fail:function(res){
        console.log(res)
        _this.setData({
          searchCont: '搜索附近店铺'
        })
      }
    })
  },

})